libpcap, 32-bit&64-bit

 情况是这样的,在之前讲过的回播 .pcap 数据的 Velodyne_player 程序中,需要调用 Winpcap (其实就是 libpcap 的 Win挫版) 的 API 解析 .pcap 数据,再通过 UDP 发送出去。我们的 Velodyne_player 是一个 Win32 的程序,显然调用的就是32位的 Winpcap 库的 API; 后来我们也移植了一个 .pcap 采集程序的 Linux 版本,结果,用该 Linux 版本采集程序采集到的 .pcap 数据却没办法用我们 Win 下的 Velodyne_player 回播。后来发现,我们的 Linux 版本的采集程序用的是64位的 libpcap 库(因为系统是64位的 Ubuntu16.04,默认安装的就是64位的 libpcap 库),64位和32位的 libpcap,在时间戳上有很关键的区别,下面是开源的 pcap.h 中的声明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
* Generic per-packet information, as supplied by libpcap.
*
* The time stamp can and should be a "struct timeval", regardless of
* whether your system supports 32-bit tv_sec in "struct timeval",
* 64-bit tv_sec in "struct timeval", or both if it supports both 32-bit
* and 64-bit applications. The on-disk format of savefiles uses 32-bit
* tv_sec (and tv_usec); this structure is irrelevant to that. 32-bit
* and 64-bit versions of libpcap, even if they're on the same platform,
* should supply the appropriate version of "struct timeval", even if
* that's not what the underlying packet capture mechanism supplies.
*/
struct pcap_pkthdr {
struct timeval ts; /* time stamp */
bpf_u_int32 caplen; /* length of portion present */
bpf_u_int32 len; /* length this packet (off wire) */
};

 对于上面遇到的问题,我们想到的解决办法是,移植的 Linux 版本采集程序生成32位的,而不是目前的64位。因为现在装的是64位的 libpcap(64位系统使然),那接下来的问题便是,如何在64位的 Ubuntu16.04 上面安装32位的 libpcap 库,然后生成我们需要的32位数据采集程序(-m32 or -m64)。

Generate 32-bit exes depending on libpcap

  1. 64位系统默认安装的包都是64位的,所以,64位的 Ubuntu16.04 安装64位的 libpcap 库比较简单。

    Reference: How can I install libpcap header files on Ubuntu 12.04?

    1
    xxx@...$ sudo apt-get install libpcap0.8 libpcap0.8-dev libpcap-dev
  2. 64位的 Ubuntu16.04 安装32位的 libpcap 库则比较麻烦一点。

    Reference: libpcap 32 bit on 64 bit Ubuntu

    1
    2
    3
    4
    5
    6
    # enable multiarch support for i386 architecture
    xxx@...$ sudo dpkg --add-architecture i386
    # update the source
    xxx@...$ sudo apt-get update
    # now, you can install the pcap library
    xxx@...$ sudo apt-get install libpcap0.8:i386 libpcap0.8-dev:i386 libpcap-dev:i386
  3. 装了32位的 libpcap 库,并不意味着你就能编译32位的,libpcap 应用程序,因为,64位系统默认并没有32位应用程序需要的基本库(诸如C/C++标准库),所以需要安装好基本库环境。

    Reference: 14.04.01 32-bit: Missing g++ 64-bit include files when cross-compiling

    1
    xxx@...$ sudo apt-get install g++-multilib
  4. 安装好32位的基本库,还有 libpcap 后,我们就能生成需要的32位程序了。步骤很简单,源代码不需要修改,在 CodeBlocks 中只需要在项目对应的 Build Option 中将 -m64 调整为 -m32;对于其他构建方式(如 cmake),方法应该类似,核心都是 -m32 还是 -m64 作为构建参数。

Linux 下如何查看程序/库信息

1.查看导出信息

1
2
3
4
xxx@...$ nm -D xxx.so/xxx.out
xxx@...$ nm -g xxx.a
# or using objdump
xxx@...$ objdump -tT xxx.so

nm [option]:
-A 在每个符号信息的前面打印所在对象文件名称;
-C 输出demangle过了的符号名称;
-D 打印动态符号;
-l 使用对象文件中的调试信息打印出所在源文件及行号;
-n 按照地址/符号值来排序;
-u 打印出那些未定义的符号

2.查看依赖库信息

1
2
# 查看动态依赖库信息
xxx@...$ ldd xxx.so/xxx.out

3.查看程序/库位数

1
xxx@...$ objdump -a xxx.a/xxx.so/xxx.out

输出结果: elf32-i386或elf64-x86-64

文章目录
  1. 1. Generate 32-bit exes depending on libpcap
  2. 2. Linux 下如何查看程序/库信息